﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;

namespace Super.Common.Cookie
{







    public class CookieHelp
    {

        /// <summary>
        /// 用户 Cookie 存储键
        /// </summary>
        private const string CookieUser = "bangke_user";

        /// <summary>
        /// 用户Cookie名称键
        /// </summary>
        private const string CookieUserName = "userName";

        /// <summary>
        /// 用户Cookie编号键
        /// </summary>
        private const string CookieUid = "uid";

        /// <summary>
        /// 用户Cookie主键值
        /// </summary>
        private const string CookieId = "id";

        /// <summary>
        /// 用户邮箱
        /// </summary>
        private const string CokieEmail = "email";

        /// <summary>
        /// 用户手机号码
        /// </summary>
        private const string CokieMobilephone = "mobilephone";


      

        public static void SetCookie(string userName,string uid,string id,string email,string mobilephone, int time)
        {
            HttpCookie cookie = new HttpCookie(CookieUser);
            cookie.Values.Add(CookieUserName, userName);
            cookie.Values.Add(CookieUid,uid);
            cookie.Values.Add(CookieId,id);
            cookie.Values.Add(CokieEmail, email);
            cookie.Values.Add(CokieMobilephone, mobilephone);

            switch (time)
            {
                case 0:
                    cookie.Expires = DateTime.Now; //即时
                    break;
                case 1:
                    cookie.Expires = DateTime.Now.AddDays(1); //一天
                    break;
                case 2:
                    cookie.Expires = DateTime.Now.AddDays(7); //七天，一周
                    break;
                case 3:
                    cookie.Expires = DateTime.Now.AddMonths(1); //一月
                    break;
                case 4:
                    cookie.Expires = DateTime.Now.AddYears(1); //一年
                    break;

            }
            HttpContext.Current.Response.Cookies.Remove(CookieUser);
           
            HttpContext.Current.Response.AppendCookie(cookie);

        }

        /// <summary>
        /// 获得登录用户主键
        /// </summary>
        /// <returns></returns>
        public static string GetId()
        {
            
           string id= HttpContext.Current.Request.Cookies[CookieUser].Values[CookieId];
           return id;
        }

        /// <summary>
        /// 获得登录用户编号
        /// </summary>
        /// <returns></returns>
        public static string GetUid()
        {
            string uid = HttpContext.Current.Request.Cookies[CookieUser].Values[CookieUid];
            return uid;
        }

        /// <summary>
        /// 获得登录用户名
        /// </summary>
        /// <returns></returns>
        public static string GetUserName()
        {
            string uid = HttpContext.Current.Request.Cookies[CookieUser].Values[CookieUserName];
            return uid;
        }

        /// <summary>
        /// 获得登录用户手机
        /// </summary>
        /// <returns></returns>
        public static string GetMobilephone()
        {
            string uid = HttpContext.Current.Request.Cookies[CookieUser].Values[CokieMobilephone];
            return uid;
        }

        /// <summary>
        /// 获得登录用户手机
        /// </summary>
        /// <returns></returns>
        public static string GetEmail()
        {
            string uid = HttpContext.Current.Request.Cookies[CookieUser].Values[CokieEmail];
            return uid;
        }

    }
}
